package com.yanqu.road.server.cmd.activity.common;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.common.CommonActivityModule;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_COMMON_ACTIVITY_AUTO_BUY,desc = "万能活动自动购买")
public class CommonActivityAutoBuyMallCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityAutoBuyReq req = CommonActivityProto.CommonActivityAutoBuyReq.parseFrom(packet.getBytes());
        activityId = req.getActivityId();

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = player.getModule(CommonActivityModule.class).autoBuyCommonActivityItem(activityId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0){
            CommonActivityProto.CommonActivityAutoBuyResp.Builder builder = CommonActivityProto.CommonActivityAutoBuyResp.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_COMMON_ACTIVITY_AUTO_BUY, builder);
        }
    }
}
